from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response

# Create your views here.
from RBAC.models import Staff
from engine.models import Flow, Node, NodeAttribute, AttrInstance, FlowInstance, AttrType


class FlowView(APIView):
    # 获取所有流程
    def get(self, request):
        try:
            data = Flow.objects.all()
        except Exception as e:
            print(e)
            return Response({'code': 400, 'msg': '获取所有流程失败'})
        _list = []
        for i in data:
            _list.append({
                'id': i.id,
                'fname': i.fname,
                'status': i.status,
            })
        return Response({
            'code': 200,
            'msg': '获取成功',
            'data': _list
        })

    # 添加流程
    def post(self, request):
        fname = request.data.get('fname')
        status = request.data.get('status')
        if not fname:
            return Response({'code': 400, 'msg': '参数错误'})
        if Flow.objects.filter(fname=fname):
            return Response({'code': 400, 'msg': '流程已存在'})
        try:
            Flow.objects.create(fname=fname, status=status)
        except Exception as e:
            print(e)
            return Response({'code': 200, 'msg': '流程添加失败'})
        return Response({'code': 201, 'msg': '流程添加成功'})


class FlowInfo(APIView):
    # 获取单个流程
    def get(self, request, id):
        try:
            data = Flow.objects.get(id=id)
        except Exception as e:
            print(e)
            return Response({'code': 400, 'msg': '流程信息不存在'})
        return Response({
            'code': 200,
            'msg': '获取单个流程成功',
            'data': {
                'id': data.id,
                'fname': data.fname,
                'status': data.status
            }
        })

    # 修改流程
    def put(self, request, id):
        fname = request.data.get('fname')
        status = request.data.get('status', False)

        if not fname:
            return Response({'code': 400, 'msg': '参数错误'})
        flow = Flow.objects.filter(fname=fname)
        if flow[0].id != id:
            return Response({'code': 400, 'msg': '流程已存在'})
        if not Flow.objects.filter(id=id):
            return Response({'code': 400, 'msg': '流程不存在'})
        try:
            Flow.objects.filter(id=id).update(fname=fname, status=status)
        except Exception as e:
            print(e)
            return Response({'code': 400, 'msg': '流程修改失败'})
        return Response({'code': 201, 'msg': '流程修改成功'})

    # 删除流程
    def delete(self, request, id):
        if not Flow.objects.filter(id=id):
            return Response({'code': 400, 'msg': '流程不存在'})
        try:
            Flow.objects.filter(id=id).delete()
        except Exception as e:
            return Response({'code': 400, 'msg': '流程删除失败'})
        return Response({'code': 204, 'msg': '流程删除成功'})


class NodeView(APIView):
    # 获取所有流程节点
    def get(self, request):
        try:
            data = Node.objects.all()
        except Exception as e:
            print(e)
            return Response({'code': 400, 'msg': '获取所有流程失败'})
        _list = []
        for i in data:
            _list.append({
                'id': i.id,
                'nname': i.nname,
                'flow': i.flow.fname,
            })
        return Response({
            'code': 200,
            'msg': '获取成功',
            'data': _list
        })

    # 添加流程节点
    def post(self, request):
        nname = request.data.get('nname')
        flow = request.data.get('flow')

        if not nname:
            return Response({'code': 400, 'msg': '节点不能为空'})

        if not Flow.objects.filter(id=flow):
            return Response({'code': 400, 'msg': '流程不存在'})
        try:
            Node.objects.create(nname=nname, flow_id=flow)
        except Exception as e:
            print(e)
            return Response({'code': 400, 'msg': '添加流程节点失败'})
        return Response({'code': 201, 'msg': '添加成功'})


class NodeInfo(APIView):
    # 获取单个节点
    def get(self, request, id):
        try:
            data = Node.objects.get(id=id)
        except Exception as e:
            print(e)
            return Response({'code': 400, 'msg': '节点信息不存在'})
        return Response({
            'code': 200,
            'msg': '获取单个节点成功',
            'data': {
                'id': data.id,
                'nname': data.nname,
                'flow': data.flow_id
            }
        })

    # 修改流程节点
    def put(self, request, id):
        nname = request.data.get('nname')
        flow=request.data.get('flow')

        if not nname:
            return Response({'code': 400, 'msg': '参数错误'})
        node = Node.objects.filter(nname=nname,flow_id=flow)
        if node :
            return Response({'code': 400, 'msg': '节点已存在'})
        if not Flow.objects.filter(id=id):
            return Response({'code': 400, 'msg': '节点不存在'})
        try:
            Node.objects.filter(id=id).update(nname=nname)
        except Exception as e:
            print(e)
            return Response({'code': 400, 'msg': '节点修改失败'})
        return Response({'code': 201, 'msg': '节点修改成功'})

    # 删除流程节点
    def delete(self, request, id):
        if not Node.objects.filter(id=id):
            return Response({'code': 400, 'msg': '节点不存在'})
        try:
            Node.objects.filter(id=id).delete()
        except Exception as e:
            return Response({'code': 400, 'msg': '节点删除失败'})
        return Response({'code': 204, 'msg': '节点删除成功'})

class AttrTypeInfo(APIView):
    # 获取节点属性类型
    def get(self,request):
        try:
            data=AttrType.objects.all()
        except Exception as e:
            print(e)
            return Response({'code': 400, 'msg': '属性类型信息不存在'})
        _list = []
        for i in data:
            _list.append({
                'id': i.id,
                'title': i.title,
            })
        return Response({
            'code': 200,
            'msg': '获取成功',
            'data': _list
        })
class NodeAttrView(APIView):
    def get(self,request):
        try:
            data=NodeAttribute.objects.all()
        except Exception as e:
            print(e)
            return Response({'code': 400, 'msg': '节点信息不存在'})
        _list = []
        for i in data:
            _list.append({
                'id': i.id,
                'aname': i.aname,
                'type': i.type.title,
                'node':i.node.nname
            })
        return Response({
            'code': 200,
            'msg': '获取成功',
            'data': _list
        })
    # 添加流程节点属性
    def post(self, request):
        aname = request.data.get('aname')
        type = request.data.get('type')
        node=request.data.get('node')
        if not all([aname, type,node]):
            return Response({'code': 400, 'msg': '属性和类型不能为空'})
        if not Node.objects.filter(id=node):
            return Response({'code': 400, 'msg': '节点不存在'})
        try:
            NodeAttribute.objects.create(aname=aname, type_id=type, node_id=node)
        except Exception as e:
            print(e)
            return Response({'code': 400, 'msg': '节点属性添加失败'})
        return Response({'code': 201, 'msg': '添加成功'})



class NodeAttrInfo(APIView):
    # # 获取单个
    # def get(self,request,id):

    # 删除流程节点属性
    def delete(self, request, id):
        if not NodeAttribute.objects.filter(id=id):
            return Response({'code': 400, 'msg': '节点属性不存在'})
        try:
            NodeAttribute.objects.filter(id=id).delete()
        except Exception as e:
            return Response({'code': 400, 'msg': '节点属性删除失败'})
        return Response({'code': 204, 'msg': '节点属性删除成功'})


class FlowInstanceView(APIView):
    # 获取当前用户发起的所有流程
    def get(self, request):
        try:
            id = request.user_info.get('id')
            user = Staff.objects.get(id=id)
        except Exception as e:
            print('>>>>>', e)
            return Response({'msg': '用户不存在或未登录', 'code': '403'})
        # data = FlowInstance.objects.filter(staff_id=id)
        data = user.flowinstance_set.all()
        if not data:
            return Response({'code': 400, 'msg': '未发起任何流程'})
        _list = []
        for i in data:
            _list.append({
                'id': i.id,
                'staff': i.staff.name,
                'flow': i.flow.fname,
                'time':str(i.created_time)
            })
        return Response({
            'code': 200,
            'msg': '获取成功',
            'data': _list
        })

    # 创建流程实例
    def post(self, request):
        try:
            id = request.user_info.get('id')
            user = Staff.objects.get(id=id)
        except Exception as e:
            print('>>>>>', e)
            return Response({'msg': '用户不存在或未登录', 'code': '403'})
        flow = request.data.get('flow')
        try:
            FlowInstance.objects.create(staff_id=id, flow_id=flow)
        except Exception as e:
            print(e)
            return Response({'code': 400, 'msg': '创建流程实例失败'})
        return Response({'code': 200, 'msg': '创建流程实例成功'})


class FlowInstanceInfo(APIView):
    # 获取单个流程实例
    def get(self, request, id):
        try:
            uid = request.user_info.get('id')
            user = Staff.objects.get(id=uid)
        except Exception as e:
            print('>>>>>', e)
            return Response({'msg': '用户不存在或未登录', 'code': '403'})
        try:
            data = FlowInstance.objects.get(id=id)
        except Exception as e:
            print(e)
            return Response({'code': 400, 'msg': '该流程不存在'})
        return Response({
            'code': 200,
            'msg': '获取成功',
            'data': {
                'id': data.id,
                'staff': data.staff.name,
                'flow': data.flow.fname,
            }
        })


class AttrInstanceView(APIView):
    # 添加流程节点属性实例
    def post(self, request):
        iname = request.data.get('iname')
        flow_instance = request.data.get('flow_instance')
        attr = request.data.get('attr')
        if not iname:
            return Response({'code': 400, 'msg': '参数错误'})
        if NodeAttribute.objects.filter(id=attr):
            return Response({'code': 400, 'msg': '节点属性不存在'})
        try:
            AttrInstance.objects.create(iname=iname, attr_id=attr, flow_instance_id=flow_instance)
        except Exception as e:
            print(e)
            return Response({'code': 400, 'msg': '属性实例添加失败'})
        return Response({'code': 200, 'msg': '添加成功'})
